﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;

using Wind.Utils;
using Wind.MultimediaStation.WebService.Entity;
using Dundas.Charting.WebControl;

namespace Wind.MultimediaStation.WebService
{
    public class DrawRealTimeChart : BaseChart
    {
        public virtual Chart GetChart(StockQuote StockQuote)
        {
            //chart 基本信息
            Chart myChart = new Chart();

            try {
                myChart.Width = 1920;
                myChart.Height = 1080;
                myChart.BackColor = Color.Black;
                myChart.Palette = ChartColorPalette.Vegas;
                myChart.Legends["Default"].Enabled = false;

                if (StockQuote!=null)
                {
                    //过滤行情数据
                    FilterQuoteData(StockQuote);

                    #region titles
                    DrawTitles(StockQuote, myChart);
                    #endregion

                    #region ChartAreas
      
                    DrawChartAreas(StockQuote, myChart);
                    #endregion

                    //处理行情曲线
                    DrawStockQuoteSeries(StockQuote, myChart);

                    //处理昨日收盘价曲线
                    DrawYestodayPriceSeries(StockQuote, myChart);
                }

            }
            catch (Exception e) 
            {
                LogHelper.Write(e.Message,e);
            }

            return myChart;
        }

        private static void DrawStockQuoteSeries(StockQuote StockQuote, Chart myChart)
        {
            Series SeriesStockQuote = new Series("StockQuote");
            myChart.Series.Add(SeriesStockQuote);
            SeriesStockQuote.BorderColor = Color.White;
            SeriesStockQuote.BorderWidth = 3;
            SeriesStockQuote.BackGradientEndColor = ColorTranslator.FromHtml("100,3,39,110");//050808 ColorTranslator.FromHtml("150, 255, 255, 255") #0A0F17
            SeriesStockQuote.Color = ColorTranslator.FromHtml("255,3,39,110");//0000FF #01286E
            SeriesStockQuote.Type = SeriesChartType.SplineArea;
            SeriesStockQuote.CustomAttributes = "DrawingStyle=LightToDark";
            SeriesStockQuote.BackGradientType = GradientType.TopBottom;
            SeriesStockQuote.ChartArea = "Default";            

            if (StockQuote!=null)
            {
            SeriesStockQuote.Points.DataBindXY(StockQuote.QuoteDateList, StockQuote.QuotePriceList);
            }            
            
            //处理休市时的时间点
            CustomerPauseLabel(StockQuote, SeriesStockQuote);
            
        }

        private static void DrawYestodayPriceSeries(StockQuote StockQuote, Chart myChart)
        {

            Series seriesYestodayPrice = new Series("YestodayPrice");
            myChart.Series.Add(seriesYestodayPrice);
            seriesYestodayPrice.Type = SeriesChartType.Line;
            seriesYestodayPrice.Color = ColorTranslator.FromHtml("220, 255, 255, 255");//150
            seriesYestodayPrice.BorderWidth = 3;
            seriesYestodayPrice.BorderStyle = ChartDashStyle.Solid;
            seriesYestodayPrice.ChartArea = "Default";

            List<string> QuoteDateListTemplate = CreateQuoteDateListTemplate(StockQuote);
            if (QuoteDateListTemplate != null)
            {
                for (int i = 0; i < QuoteDateListTemplate.Count; i++)
                {
                    double yestodayPrice = StockQuote.YestodayPrice;
                    seriesYestodayPrice.Points.AddXY(QuoteDateListTemplate[i], yestodayPrice);   
                }
            }

        }

        private static void DrawChartAreas(StockQuote StockQuote, Chart myChart)
        {
            //处理平均值
            if (StockQuote!=null)
            {
                ChartArea ChartArea = new ChartArea();
                ChartArea.Name = "Default";
                myChart.ChartAreas.Add(ChartArea);
                ChartArea.BackColor = Color.Black;

                //计算Y值区域
                ComputeYArea(StockQuote, ChartArea);
                ChartArea.AxisY.LabelStyle.Font = new Font("Arial", 20f, FontStyle.Regular);
                ChartArea.AxisY.MajorGrid.LineWidth = 3;
                

                //设置 开市 收市时间
                
                //ChartArea.AxisX.Minimum = StockQuote.QuoteStartTime.ToOADate();
                //ChartArea.AxisX.Maximum = StockQuote.QuoteEndTime.ToOADate(); 

                ChartArea.AxisX.LabelsAutoFit = false;
                //ChartArea.AxisX.LabelStyle.Font = new Font("Trebuchet MS", 8.25F, FontStyle.Bold);
                ChartArea.AxisX.Margin = false;
                ChartArea.AxisX.MajorGrid.Enabled = false;
                ChartArea.AxisX.MinorGrid.Enabled = false;
                ChartArea.AxisX.Interval = 60f;
                //ChartArea.AxisX.IntervalType = DateTimeIntervalType.Minutes;
                ChartArea.AxisX.LabelStyle.FontColor = Color.White;
                ChartArea.AxisX.LabelStyle.Font=new Font("Arial",26f,FontStyle.Regular);
                //ChartArea.AxisX.LabelStyle.Format = "hh:mm";                

                ChartArea.AxisX.LineColor = Color.White;
                ChartArea.AxisX.LineWidth=4;
                ChartArea.AxisX.MajorTickMark.LineColor = Color.White;

                ChartArea.Position.X = 0f;
                ChartArea.Position.Y = 20f;
                ChartArea.Position.Width = 95f;
                ChartArea.Position.Height =70f;

            }            

        }

        private static void DrawTitles(StockQuote StockQuote, Chart myChart)
        {
            Title titleTop = new Title();
            titleTop.Name = "Top";
            titleTop.Font = new Font("黑体", 100f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleTop.Position.X = 0f;
            titleTop.Position.Y = 0f;
            titleTop.Position.Width = 100f;
            titleTop.Position.Height = 15f;
            titleTop.Color = Color.White;
            titleTop.Docking = Docking.Top;
            titleTop.Alignment = ContentAlignment.TopLeft;
            titleTop.BackImage = "Picture/ChartBg/top.png";
            
            float topHeight = 0;//titleTop.Position.Height;

            Title titleShortName = new Title();
            titleShortName.Name = "ShortName";
            titleShortName.Font = new Font("黑体", 100f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleShortName.Position.X = 2f;
            titleShortName.Position.Y = 4f + topHeight;
            titleShortName.Position.Width =25f;
            titleShortName.Position.Height = 12f;
            titleShortName.Color = Color.White;
            titleShortName.Docking = Docking.Top;
            titleShortName.Alignment = ContentAlignment.TopLeft;

            Title titleCurrentPrice = new Title();
            titleCurrentPrice.Name = "CurrentPrice";
            titleCurrentPrice.Font = new Font("Arial", 90f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleCurrentPrice.Position.X = titleShortName.Position.X + titleShortName.Position.Width;
            titleCurrentPrice.Position.Y = 4f + topHeight;
            titleCurrentPrice.Position.Width = 22f;
            titleCurrentPrice.Position.Height = 12f;
            titleCurrentPrice.Color = Color.White;

            Title titleCurrentDate = new Title();
            titleCurrentDate.Name = "CurrentDate";
            titleCurrentDate.Font = new Font("宋体", 60f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleCurrentDate.Position.X = titleCurrentPrice.Position.X + titleCurrentPrice.Position.Width;
            titleCurrentDate.Position.Y = 8f + topHeight;
            titleCurrentDate.Position.Width = 20f;
            titleCurrentDate.Position.Height = 10f;
            titleCurrentDate.Color = Color.White;

            Title titleSwing = new Title();
            titleSwing.Name = "Swing";
            titleSwing.Font = new Font("Arial", 70f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleSwing.Position.X = titleCurrentPrice.Position.X + titleCurrentPrice.Position.Width+2f;
            titleSwing.Position.Y = 1f + topHeight;
            titleSwing.Position.Width = 18f;
            titleSwing.Position.Height = 10f;
            titleSwing.Color = Color.White;

            Title titleSwingPercent = new Title();
            titleSwingPercent.Name = "SwingPercent";
            titleSwingPercent.Font = new Font("Arial", 70f, FontStyle.Bold, GraphicsUnit.Pixel);
            titleSwingPercent.Position.X =75 ;//titleSwing.Position.X + titleSwing.Position.Width+0.5f;
            titleSwingPercent.Position.Y = 2f + topHeight;
            titleSwingPercent.Position.Width = 15f;
            titleSwingPercent.Position.Height = 15f;
            titleSwingPercent.Color = Color.White;

#if DEBUG
            //titleShortName.BackColor = Color.Yellow;
            //titleCurrentPrice.BackColor = Color.Blue;
            //titleCurrentDate.BackColor = Color.Gray;
            //titleSwing.BackColor = Color.Red;
            //titleSwingPercent.BackColor = Color.YellowGreen;
#endif

            //myChart.Titles.Add(titleTop);
            myChart.Titles.Add(titleShortName);
            myChart.Titles.Add(titleCurrentPrice);
            myChart.Titles.Add(titleCurrentDate);
            myChart.Titles.Add(titleSwing);
            myChart.Titles.Add(titleSwingPercent);

            if (StockQuote!=null)
            {
                myChart.Titles["ShortName"].Text = StockQuote.ShortName;
                myChart.Titles["CurrentPrice"].Text = StockQuote.CurrentPrice;
                myChart.Titles["CurrentDate"].Text = string.Format("截止：{0}",StockQuote.CurrentDate);
                myChart.Titles["Swing"].Text = StockQuote.Swing;
                myChart.Titles["SwingPercent"].Text = StockQuote.SwingPercent;
                if (StockQuote.IsDown)
                {
                    myChart.Titles["Swing"].BackImage = "Picture/ChartBg/diearrow.png";
                    myChart.Titles["SwingPercent"].BackImage = "Picture/ChartBg/die.png";
                    myChart.Titles["CurrentPrice"].Color = ColorTranslator.FromHtml("#00FF00");

                }
                else
                {
                    myChart.Titles["Swing"].BackImage = "Picture/ChartBg/zhangarrow.png";
                    myChart.Titles["SwingPercent"].BackImage = "Picture/ChartBg/zhang.png";
                    myChart.Titles["CurrentPrice"].Color = ColorTranslator.FromHtml("#FF0000");
                }
            }
            
        }
    }

}
